<?php
namespace tp5_lib\access\logic;

use tp5_lib\common\logic\Logic;
use tp5_lib\access\model\RoleModel;

/**
 * 角色
 */
class RoleLogic extends Logic
{
    static protected $_instance=null;
    static public function instance()
    {
        if(static::$_instance === null) static::$_instance = new self;
        return static::$_instance;
    }
    /** @return RoleModel */
    public function model()
    {
        if(empty($this->_model)){
            $this->_model=new RoleModel();
        }
        return $this->_model;
    }
    public function db()
    {
        return $this->model()->db();
    }
    /** @return PrivilegeLogic */
    public function privilege()
    {
        if(empty($this->_privilege)){
            $this->_privilege=PrivilegeLogic::instance();
        }
        return $this->_privilege;
    }
    /** 获取角色列表 */
    public function getAllRoles()
    {
        if(empty($this->_data)){
            $list=$this->db()->select();
            $data=[];
            foreach($list as $item){
                $data[$item["code"]]=$item;
            }
            $this->_data=$data;
        }
        return $this->_data;
    }
    /** 刷新角色列表 */
    public function refresh()
    {
        unset($this->_data);
    }
    /** 
     * 返回已有角色名
     * @return string 角色名，多个用`,`分隔
     */
    public function getRoleNames()
    {
        if(empty($this->_roles)) $this->_roles=session("role");
        return $this->_roles;
    }
    public function setRoleNames($roles,$save=true)
    {
        if($save) session("role",$roles);
        $this->_roles=$roles;
        return $this;
    }
    /** 清空用户角色 */
    public function clear()
    {
        $this->_roles=null;
        session("role",null);
        return $this;
    }
    /**
     * 返回已有角色
     * @return array 索引数组
     */
    public function getRoles()
    {
        $names=$this->getRoleNames();
        $names=explode(",",$names);
        $roles=$this->getAllRoles();
        $list=[];
        foreach($names as $name){
            if(isset($roles[$name])){
                $list[$name]=$roles[$name];
            }
        }
        return $list;
    }
    /**
     * 加载角色
     * @param string $roles 角色名，多个用`,`分隔
     */
    public function load($roles)
    {
        $roles=explode(",",$roles);
        $data=$this->getAllRoles();
        foreach($roles as $index => $code){
            if(isset($data[$code])){
                $role=$data[$code];
                if($role["parent"] && !in_array($role["parent"],$roles)){
                    array_unshift($roles,$role["parent"]);
                }
            }
            else{
                unset($roles[$index]);
            }
        }
        
        $privilege=$this->privilege();
        foreach($roles as $code){
            $privilege->accredit($data[$code]["privilege"]);
        }
        $roles=implode(",",$roles);
        $this->setRoleNames($roles);
        return $this;
    }
    /**
     * 检查角色
     * @param string $role
     */
    public function check($role)
    {
        if(empty($role)) return true;
        $roles=explode(",",$this->getRoleNames());
        return in_array($role,$roles);
    }
}
